<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 警告 -->
      <el-alert title="注意:只允许为第三级分类设置相关参数" type="warning" show-icon>
      </el-alert>
      <!-- 级联选择器 -->
      <div class="chios">
        选择商品分类：<el-cascader v-model="typeArr" :options="options" @change="chgCascader"
          :props="{ expandTrigger: 'hover', label: 'cat_name', value: 'cat_id' }"></el-cascader>
      </div>
      <!-- 标签页 -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="动态参数" name="many">
          <!-- 按钮 -->
          <el-button :disabled="typeArr.length !== 3" type="primary" size="mini">添加参数</el-button>
          <!-- 表格 -->
          <el-table border style="width: 100%">
            <el-table-column prop="date" label="" width="80">
            </el-table-column>
            <el-table-column prop="name" label="" width="80">
            </el-table-column>
            <el-table-column prop="address" label="参数名">
            </el-table-column>
            <el-table-column prop="address" label="操作">
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="noly">
          <el-button :disabled="typeArr.length !== 3" type="primary" size="mini"
            @click="centerDialogVisible = true">添加参数</el-button>
          <el-dialog title="添加角色" width="50%" :visible.sync="centerDialogVisible">
            <el-form-item label="静态属性" label-width="100px">
              <el-form-item label="参数" label-width="100px">
                <el-input> </el-input>
              </el-form-item>
            </el-form-item>
            <!-- 具名插槽 -->
            <span slot="footer" class="dialog-footer">
              <el-button @click="centerDialogVisible = false">取 消</el-button>
              <el-button type="primary">确 定</el-button>
            </span>
          </el-dialog>
          <!-- 表格 -->
          <el-table border style="width: 100%" :data="nolyData">
            <el-table-column type="expand" label="展示" width="80">
              <!-- <template slot-scope="scope"> -->
              <el-tag v-for="tag in nolyData" :key="tag.qttr_id" closable>
                {{ tag.attr_vals }}
              </el-tag>
              <!-- </template> -->
            </el-table-column>
            <el-table-column type="index" label="" width="80">
            </el-table-column>
            <el-table-column prop="attr_name" label="属性名称">
            </el-table-column>
            <el-table-column prop="address" label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delParamsId(scope.row.attr_id)">删除</el-button>
              </template>

            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  data () {
    return {
      // 默认标签分页
      activeName: "many",
      // 联级数据
      options: [],
      // 参数数组
      typeArr: [],
      centerDialogVisible: false,
      // 静态属性
      nolyData: []
    }
  },
  mounted () {
    this.getCategoriesCascader()
  },
  methods: {
    // 获取级联数据 分类数据
    async getCategoriesCascader () {
      const { data: { data, meta } } = await request({
        url: 'categories',
        method: 'GET'
      })
      if (meta.status !== 200) {
        return this.$message.error("获取参数错误")
      }
      this.options = data
      console.log("---------", data)
    },
    chgCascader (val) {
      if (val.length !== 3) {
        this.typeArr = []
        return
      }
      // 获取参数
      this.getCategoriesAttributes()
      this.getCategoriesAttrOnly()
    },
    getCategoriesAttributes () {
      request({
        url: `categories/${this.typeArr[2]}/attributes`,
        params: { sel: this.activeName },
        method: "GET"
      })
    },
    // 添加静态参数
    // 获取静态参数
    async getCategoriesAttrOnly () {
      const { data: { data, meta } } = await request({
        url: `categories/${this.typeArr[2]}/attributes`,
        params: {
          sel: 'only'
        },
        method: 'GET'
      })
      if (meta.status !== 200) {
        // 给出提示
        return this.$message.error("获取商品属性错误")
      }
      // data是数组
      console.log("1111111111", data)
      this.nolyData = data
      this.$message.success("获取数据成功")
    },
    // 删除数据
    async delParamsId (id) {
      try {
        await this.$confirm('确定删除吗，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // 往后台数据
        const { data: { meta } } = await request({
          url: `categories/${this.typeArr[2]}/attributes/${id}`,
          method: 'delete'
        })
        if (meta.status !== 200) {
          return this.$message.error("删除失败")
        }
        // 删除成功 重新获取数据 刷新页面
        this.getCategoriesAttrOnly()
        this.$message.success("删除成功")
      } catch (error) {
        return this.$message.info("取消删除")
      }
    }
  }
}
</script>

<style lang="less" scoped>
.box-card {
  margin-top: 20px;

  .chios {
    font-family: "宋体";
    font-style: italic;
    margin-top: 10px;
    font-size: 20px;
  }

  .el-table {
    margin-top: 20px;
  }
}
</style>
